﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using YawetagDemo.Framework.Data;
using YawetagDemo.Framework.Server.Data;
using YawetagDemo.Framework.Server.Services;
using YawetagDemo.Framework.Services;
using YawetagLib.System.Data;
using YawetagLib.System.ServiceModel;
using YawetagLib.System.Utilities;

namespace YawetagDemo.Framework.Client.Services
{
    public class WCFMusicServiceProxy : ServiceProxy<IWCFMusicService>, IMusicService
    {
        public WCFMusicServiceProxy()
        {
            ChannelFactory = new DuplexChannelFactory<IWCFMusicService>(new WCFMusicServiceCallbacks(this), new NetTcpBinding(SecurityMode.None, true));
            Service = ChannelFactory.CreateChannel(new EndpointAddress("net.tcp://localhost:3000/MusicService"));
        }

        public IArtist GetArtist(int artistID)
        {
            return Service.GetArtist(artistID);
        }

        public IList<IArtist> GetArtists()
        {
            return Service.GetArtists().OfType<IArtist>().ToList();
        }

        public IArtist CreateArtist(string name, DateTime? dob)
        {
            return Service.CreateArtist(name, dob);
        }

        public IArtist UpdateArtist(int artistID, string name, DateTime? dob)
        {
            return Service.UpdateArtist(artistID, name, dob);
        }

        public void DeleteArtist(int artistID)
        {
            Service.DeleteArtist(artistID);
        }

        public IBand GetBand(int bandID)
        {
            return Service.GetBand(bandID);
        }

        public IList<IBand> GetBands()
        {
            return Service.GetBands().OfType<IBand>().ToList();
        }

        public IBand CreateBand(string name)
        {
            return Service.CreateBand(name);
        }

        public IBand UpdateBand(int bandID, string name)
        {
            return Service.UpdateBand(bandID, name);
        }

        public void DeleteBand(int bandID)
        {
            Service.DeleteBand(bandID);
        }

        public void AddArtistToBand(int bandID, int artistID)
        {
            Service.AddArtistToBand(bandID, artistID);
        }

        public void RemoveArtistFromBand(int bandID, int artistID)
        {
            Service.RemoveArtistFromBand(bandID, artistID);
        }

        public IAlbum GetAlbum(int albumID)
        {
            return Service.GetAlbum(albumID);
        }

        public IAlbum CreateAlbum(int bandID, string name, DateTime releaseDate)
        {
            return Service.CreateAlbum(bandID, name, releaseDate);
        }

        public IAlbum UpdateAlbum(int albumID, string name, DateTime releaseDate)
        {
            return Service.UpdateAlbum(albumID, name, releaseDate);
        }

        public void RankAlbum(int albumID, int personID, byte stars)
        {
            Service.RankAlbum(albumID, personID, stars);
        }

        public void DeleteAlbum(int albumID)
        {
            Service.DeleteAlbum(albumID);
        }

        public void LogArtists(Func<IArtist, string> getArtist)
        {
            GetArtistForLoggingFunc = getArtist;

            Service.LogArtists();
        }

        public PagedResultInfo<IAlbumRankingItem> GetAlbumRankingItems(string filterCriteria, string sortExpression, 
            int startRowIndex, int? maximumRows)
        {
            return Service
                .GetAlbumRankingItems(filterCriteria, sortExpression, startRowIndex, maximumRows)
                .OfType<WCFAlbumRankingItem, IAlbumRankingItem>();
        }

        internal Func<IArtist, string> GetArtistForLoggingFunc { get; private set; }
    }
}
